#include "deletemonthdatadialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QMessageBox>
#include <QSqlError>
#include <QDebug>

DeleteMonthDataDialog::DeleteMonthDataDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("删除月数据");
    setFixedSize(400, 200);
    setupUI();
    populateMonthComboBox();
}

void DeleteMonthDataDialog::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout();
    
    // 月份选择
    QLabel *monthLabel = new QLabel("选择月份:");
    m_monthComboBox = new QComboBox();
    formLayout->addRow(monthLabel, m_monthComboBox);
    
    mainLayout->addLayout(formLayout);
    
    // 对话框按钮
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    m_deleteButton = m_buttonBox->button(QDialogButtonBox::Ok);
    m_deleteButton->setText("删除");
    
    mainLayout->addWidget(m_buttonBox);
    
    // 连接信号和槽
    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &DeleteMonthDataDialog::onDeleteData);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

void DeleteMonthDataDialog::populateMonthComboBox()
{
    m_monthComboBox->clear();
    
    // 添加最近12个月的选项
    QDate currentDate = QDate::currentDate();
    for (int i = 0; i < 12; i++) {
        QDate date = currentDate.addMonths(-i);
        QString monthText = date.toString("yyyy年MM月");
        QString monthValue = date.toString("yyyy-MM");
        m_monthComboBox->addItem(monthText, monthValue);
    }
}

void DeleteMonthDataDialog::onDeleteData()
{
    QString selectedMonth = m_monthComboBox->currentData().toString();
    QString monthText = m_monthComboBox->currentText();
    
    if (selectedMonth.isEmpty()) {
        QMessageBox::warning(this, "警告", "请选择要删除的月份");
        return;
    }
    
    // 确认对话框
    int result = QMessageBox::question(this, "确认删除", 
                                      "确定要删除 " + monthText + " 的所有员工生产数据吗？\n\n"
                                      "注意：此操作不可撤销！",
                                      QMessageBox::Yes | QMessageBox::No,
                                      QMessageBox::No);
    
    if (result != QMessageBox::Yes) {
        return;
    }
    
    // 解析选中的月份，构造日期范围
    QDate monthDate = QDate::fromString(selectedMonth, "yyyy-MM");
    QDate startDate = QDate(monthDate.year(), monthDate.month(), 1);
    QDate endDate = QDate(monthDate.year(), monthDate.month(), monthDate.daysInMonth());
    
    QString startDateStr = startDate.toString("yyyy-MM-dd");
    QString endDateStr = endDate.toString("yyyy-MM-dd");
    
    // 删除指定月份的所有生产数据
    // 使用日期范围查询，这种方式更可靠
    QSqlQuery query;
    query.prepare("DELETE FROM production_v2 WHERE date BETWEEN ? AND ?");
    query.addBindValue(startDateStr);
    query.addBindValue(endDateStr);
    
    if (!query.exec()) {
        QMessageBox::critical(this, "错误", "删除月数据失败: " + query.lastError().text());
        return;
    }
    
    // 由于SQLite的numRowsAffected()可能不准确，我们显示一个通用的成功消息
    QMessageBox::information(this, "成功", "成功删除 " + monthText + " 的生产数据");
    
    // 关闭对话框
    accept();
}